<?
require_once('functions.php');

function GetTreatments()
{
	$connect = GetConnection();
	return $connect->query('SELECT * FROM Psi_TreatmentOptions');
}
function BlankTreatment()
{
	return array('Diagnosis'=>null,'Treatment'=>null,'Care'=>null,'ClinicalTests'=>null,'Living'=>null,'Support'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function GetTreatment($id)
{
	$connect = GetConnection();
	$sql = "SELECT * FROM Psi_TreatmentOptions WHERE id=$id";
	$results = $connect->query($sql);
	//echo $sql; die();
	$row = $results->fetch_assoc();
	$connect->close();
	return $row;
}
function SaveTreatment($row)
{
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	$sql = 	"UPDATE Psi_TreatmentOptions "
		.	"Set "
		
		.	"Diagnosis='$escape[Diagnosis]', "
		.	"Treatment='$escape[Treatment]', "
		.	"Care='$escape[Care]', "
		.	"ClinicalTests='$escape[ClinicalTests]', "
		.	"Living='$escape[Living]', "
		.	"Support='$escape[Support]' "
		
		.	"WHERE id=$row[id]";
	$results = $connect->query($sql);
	//echo $sql;
	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function CreateTreatment($row) {
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	
	$Diagnosis 			= 	$_POST['Diagnosis'];
	$Treatment			= 	$_POST['Treatment'];
	$Care 				= 	$_POST['Care'];
	$ClinicalTests 		= 	$_POST['ClinicalTests'];
	$Living 			= 	$_POST['Living'];
	$Support			= 	$_POST['Support'];
	
	$sql = 
		"INSERT INTO Psi_TreatmentOptions (
			Diagnosis, Treatment, Care, ClinicalTests, Living, Support, created_at
		) VALUES (
			'$escape[Diagnosis]', '$escape[Treatment]', '$escape[Care]', '$escape[ClinicalTests]', '$escape[Living]', '$escape[Support]', NOW()
		)";
	
	$result = $connect->query($sql);
	if ($result) {
		// Success!
		header("Location: index.php");
		die();
	} else {
		// Display error message.
		echo "<p class=\"error\">Subject creation failed.</p>";
		echo "<p class=\"error\">" . mysql_error() . "</p>";
	}

	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function DeleteTreatment($id)
{
	$conn = GetConnection();
	$sql = 	"DELETE FROM Psi_Treatments WHERE id=$id";
	$results = $conn->query($sql);
	//echo $sql;
	$error = $conn->error;
	$conn->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}
/*function DeleteTreatment($row) {
	$connect = GetConnection();

	if (intval($_GET['id']) == 0) {
			header("Location: index.php");
			die();
	}
	
	$id = mysql_prep($_GET['id']);
	
	if ($subject = GetTreatment($id)) {
		
		$sql = "DELETE FROM Psi_TreatmentOptions WHERE id = {$id} LIMIT 1";
		$result = $connect->query($sql);
		if ( $connect->affected_rows == 1 ) {
			header("Location: index.php");
			die();
		} else {
			// Deletion Failed
			echo "<p class=\"error\">Subject deletion failed.</p>";
			echo "<p class=\"error\">" . mysql_error() . "</p>";
			echo "<a href=\"index.php\">Return to Main Page</a>";
		}
	} else {
		// Theorist didn't exist in database
			header("Location: index.php");
			die();
	}

	$connect->close();
}*/
function ValidateTreatment($row)
{
	$errors = array();
	if(empty($row['Diagnosis'])) $errors['Diagnosis'] = 'Diagnosis is required';

	return count($errors) == 0 ? true : $errors;
}